package com.post799.soundapp;

import java.util.ArrayList;
import java.util.List;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;

import org.json.JSONArray;
import org.json.JSONObject;
import org.taptwo.android.widget.TitleProvider;
import org.taptwo.android.widget.ViewFlow;
import org.taptwo.android.widget.ViewFlow.ViewSwitchListener;

import com.bappi.custom.activities.CustomActivity;
import com.bappi.utils.AnimationFactory;
import com.bappi.utils.Constants;
import com.bappi.utils.ImageUtils;
import com.bappi.utils.Utils;
import com.bappi.utils.AnimationFactory.FlipDirection;
import com.whitenoise.model.Theme;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ViewAnimator;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Tab1Activity1 extends CustomActivity {

	private static String KEY_MEDIA_PLAYER_COMMAND = "KEY_MEDIA_PLAYER_COMMAND_2";
	private static String KEY_COMMAND = "KEY_COMMAND";
	private static String KEY_TIME = "KEY_TIME";
	private static int COMMAND_COMPLETED = 0;

	private ViewAnimator viewAnimator;
	private Bitmap playImage, pauseImage, prevImage, nextImage;
	private boolean isPlaying;
	private boolean isFirstTime;

	private TextView titleView;
	private ViewFlow viewFlow;
	private List<Theme> themes;
	private ViewFlowAdapter viewFlowAdapter;
	private CatalogAdapter catalogAdapter;
	private SharedPreferences sp;
	private ListView listView;
	private RelativeLayout viewFlowLayout, listViewLayout,settingsLayout;

	public static void notifyMediaCompleted(SharedPreferences sp) {
		try {
			JSONObject jo = new JSONObject();
			jo.put(KEY_TIME, System.currentTimeMillis());
			jo.put(KEY_COMMAND, COMMAND_COMPLETED);
			sp.edit().putString(KEY_MEDIA_PLAYER_COMMAND, jo.toString())
					.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	private OnSeekBarChangeListener onSeekBarChangeListener = new OnSeekBarChangeListener() {
//
//		@Override
//		public void onStopTrackingTouch(SeekBar seekBar) {
////			sp.edit().putInt(Constants.KEY_LAST_PLAYED_SOUND_VOLUMN, volume)
////					.commit();
////			viewFlowAdapter.notifyDataSetChanged();
//			String key = "VOLUME"+themes.get(viewFlow.getSelectedItemPosition()).getSoundFile();
//			sp.edit().putInt(key, volume).commit();
//			themes.get(viewFlow.getSelectedItemPosition()).setVolume(volume);
//			
//		}
//
//		@Override
//		public void onStartTrackingTouch(SeekBar seekBar) {
//			// TODO Auto-generated method stub
//
//		}
//
//		@Override
//		public void onProgressChanged(SeekBar seekBar, int progress,
//				boolean fromUser) {
//			volume = progress;
//			themes.get(viewFlow.getSelectedItemPosition()).setVolume(volume);
//			SoundPlayerService.setVolume(sp, volume);
//		}
//	};

	private OnSharedPreferenceChangeListener ospcl = new OnSharedPreferenceChangeListener() {

		public void onSharedPreferenceChanged(final SharedPreferences sp,
				String key) {
			try {
				if (KEY_MEDIA_PLAYER_COMMAND.equals(key)) {
					String object = sp
							.getString(KEY_MEDIA_PLAYER_COMMAND, null);
					if (object != null) {
						JSONObject jo = new JSONObject(object);
						int command = jo.getInt(KEY_COMMAND);
						if (command == COMMAND_COMPLETED) {
							runOnUiThread(new Runnable() {

								@Override
								public void run() {
									try {
										isPlaying = false;
										if (viewFlowAdapter != null) {
											viewFlowAdapter
													.notifyDataSetChanged();
										}
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							});
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	private OnClickListener toggleButtonListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			togglePlayClicked();
		}
	};

	private ViewSwitchListener viewSwitchListener = new ViewSwitchListener() {

		public void onSwitched(View view, int position) {
			if (position == 0) {
				viewFlow.setSelection(viewFlowAdapter.getCount() - 2);
			} else if (position == viewFlowAdapter.getCount() - 1) {
				viewFlow.setSelection(1);
			} else {
				sp.edit()
						.putInt(Constants.KEY_LAST_PLAYED_FILE_INDEX, position)
						.commit();
				Theme theme = (Theme) viewFlowAdapter.getItem(position);
				titleView.setText(theme.getTitle());
				startAudio(theme);
			}
		}
	};

	private void startAudio(final Theme theme) {
		try {
			if (Utils.isServiceRunning(getParent(), SoundPlayerService.class)) {
				stopService(new Intent(getParent(), SoundPlayerService.class));
			}
			Intent intent = new Intent(getParent(), SoundPlayerService.class);
			intent.putExtra(Constants.KEY_AUDIO_FILE_NAME, theme.getSoundFile());
			intent.putExtra(Constants.KEY_AUDIO_SHOULD_PLAY, isPlaying);
			intent.putExtra(Constants.KEY_AUDIO_VOLUME, theme.getVolume());
//			intent.putExtra(Constants.KEY_AUDIO_SHOULD_LOOPING, isLoopingEnabled);
//			intent.putExtra(Constants.KEY_AUDIO_LOOPING_TIME, (isStopSoundInEnabled&&isTimerEnabled) ?loopingTime:Integer.MAX_VALUE);
			intent.putExtra(Constants.KEY_AUDIO_SHOULD_LOOPING, true);
			if(isLoopingEnabled){
				intent.putExtra(Constants.KEY_AUDIO_LOOPING_TIME, Constants.DEFAULT_LOOPING_TIME);
			}else{
				intent.putExtra(Constants.KEY_AUDIO_LOOPING_TIME, (isStopSoundInEnabled&&isTimerEnabled) ?loopingTime:Integer.MAX_VALUE);
			}
			
			startService(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private OnItemClickListener onItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> av, View v, int position,
				long arg) {
			viewFlow.setSelection(position + 1);
			showFlowViewFromListView();
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			setContentView(R.layout.layout_tab_1_screen_1);
			sp = PreferenceManager.getDefaultSharedPreferences(getParent());
			sp.registerOnSharedPreferenceChangeListener(ospcl);
			viewFlowLayout = (RelativeLayout) findViewById(R.id.view_flow_layout);
			listViewLayout = (RelativeLayout) findViewById(R.id.list_view_layout);
			settingsLayout = (RelativeLayout) findViewById(R.id.settings_layout);
			viewFlow = (ViewFlow) findViewById(R.id.viewflow);
			listView = (ListView) this.findViewById(R.id.list_view);
			viewAnimator = (ViewAnimator) this.findViewById(R.id.view_flipper);
			titleView = (TextView) findViewById(R.id.title_field);
			initSettingsLayout();
			themes = new ArrayList<Theme>();	
			isPlaying = true;
			isFirstTime = true;
			new AsyncTask<String, String, String>() {
				private ProgressDialog progressDialog;

				protected void onPreExecute() {
					try {
						progressDialog = Utils.getProgressDialog(getParent());
						progressDialog.show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				protected String doInBackground(String... arg0) {
					try {
						float dpiFactor = Utils.getDPIFactor(getParent());
						playImage = ImageUtils.getImage(getParent()
								.getResources(), dpiFactor,
								R.drawable.play_button);
						pauseImage = ImageUtils.getImage(getParent()
								.getResources(), dpiFactor,
								R.drawable.pause_button);
						prevImage = ImageUtils.getImage(getParent()
								.getResources(), dpiFactor,
								R.drawable.left_arrow_button);
						nextImage = ImageUtils.getImage(getParent()
								.getResources(), dpiFactor,
								R.drawable.right_arrow_button);

						JSONArray ja = Utils.getCatalogInfo(getParent());
						JSONObject jo;
						Theme theme;
						for (int i = 0; i < ja.length(); i++) {
							jo = ja.getJSONObject(i);
							theme = new Theme();
							theme.setTitle(jo.getString("title"));
							theme.setImageFile(jo.getString("image_file_name"));
							theme.setSoundFile(jo.getString("sound_file_name"));
							String key = "VOLUME"+theme.getSoundFile();
							theme.setVolume(sp.getInt(key, 100));
							themes.add(theme);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				protected void onPostExecute(String unused) {
					try {
						viewFlowAdapter = new ViewFlowAdapter();
						viewFlow.setAdapter(viewFlowAdapter);
						viewFlow.setOnViewSwitchListener(viewSwitchListener);
						viewFlow.setSelection(sp.getInt(
								Constants.KEY_LAST_PLAYED_FILE_INDEX, 1));
						catalogAdapter = new CatalogAdapter();
						listView.setAdapter(catalogAdapter);
						listView.setOnItemClickListener(onItemClickListener);
						progressDialog.dismiss();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();
			findViewById(R.id.button_catalog).setOnClickListener(
					new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							TabsController.hideTabBar(sp);
							showCatalog();
						}
					});
			findViewById(R.id.button_settings).setOnClickListener(
					new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							TabsController.hideTabBar(sp);
							showSettings();
						}
					});
			findViewById(R.id.button_back).setOnClickListener(
					new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							showFlowViewFromListView();
						}
					});
			findViewById(R.id.button_back_in_settings).setOnClickListener(
					new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							showFlowViewFromSettings();
						}
					});

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		try {
			if (sp != null && ospcl != null) {
				sp.unregisterOnSharedPreferenceChangeListener(ospcl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			if(isFirstTime){
				isFirstTime = false;
			}else{
				TabsController.hideTabBar(sp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void showCatalog() {
		try {
			final Animation[] animc = AnimationFactory.flipAnimation(
					viewFlowLayout, listViewLayout, FlipDirection.LEFT_RIGHT,
					500, null);
			viewAnimator.setOutAnimation(animc[0]);
			viewAnimator.setInAnimation(animc[1]);
			animc[1].setAnimationListener(new Animation.AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					viewAnimator.setOutAnimation(null);
					viewAnimator.setInAnimation(null);
				}
			});
			viewAnimator.setDisplayedChild(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void showSettings() {
		try {
			final Animation[] animc = AnimationFactory.flipAnimation(
					viewFlowLayout, settingsLayout, FlipDirection.LEFT_RIGHT,
					500, null);
			viewAnimator.setOutAnimation(animc[0]);
			viewAnimator.setInAnimation(animc[1]);
			animc[1].setAnimationListener(new Animation.AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					viewAnimator.setOutAnimation(null);
					viewAnimator.setInAnimation(null);
				}
			});
			viewAnimator.setDisplayedChild(2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void showFlowViewFromListView() {
		try {
			final Animation[] animc = AnimationFactory.flipAnimation(
					listViewLayout, viewFlowLayout, FlipDirection.LEFT_RIGHT,
					500, null);
			viewAnimator.setOutAnimation(animc[0]);
			viewAnimator.setInAnimation(animc[1]);

			animc[1].setAnimationListener(new Animation.AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					viewAnimator.setOutAnimation(null);
					viewAnimator.setInAnimation(null);
				}
			});
			viewAnimator.setDisplayedChild(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void showFlowViewFromSettings() {
		try {
			final Animation[] animc = AnimationFactory.flipAnimation(
					settingsLayout, viewFlowLayout, FlipDirection.LEFT_RIGHT,
					500, null);
			viewAnimator.setOutAnimation(animc[0]);
			viewAnimator.setInAnimation(animc[1]);

			animc[1].setAnimationListener(new Animation.AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					viewAnimator.setOutAnimation(null);
					viewAnimator.setInAnimation(null);
				}
			});
			viewAnimator.setDisplayedChild(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public class ViewFlowAdapter extends BaseAdapter implements TitleProvider {

		private LayoutInflater mInflater;

		public ViewFlowAdapter() {
			mInflater = (LayoutInflater) getParent().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return themes.size() + 2;
		}

		public Object getItem(int arg0) {
			return themes.get(getActualPosition(arg0));
		}

		private int getActualPosition(int arg0) {
			int ret;
			if (arg0 == 0) {
				ret = themes.size() - 1;
			} else if (arg0 == themes.size() + 1) {
				ret = 0;
			} else {
				ret = arg0 - 1;
			}
			return ret;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewFlowViewHolder holder;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.flow_item, null);
					holder = new ViewFlowViewHolder();
					holder.mainContainer = (View) convertView
							.findViewById(R.id.flow_item_layout);
					holder.previousButton = (ImageButton) convertView
							.findViewById(R.id.button_previous);
					holder.nextButton = (ImageButton) convertView
							.findViewById(R.id.button_next);
					holder.toggleButton = (ImageButton) convertView
							.findViewById(R.id.button_play_pause);
					holder.volumeBar = (SeekBar) convertView
							.findViewById(R.id.sound_volume_seek_bar);
					convertView.setTag(holder);

					holder.previousButton.setImageBitmap(prevImage);
					holder.nextButton.setImageBitmap(nextImage);
					
					holder.toggleButton
							.setOnClickListener(toggleButtonListener);

				} else {
					holder = (ViewFlowViewHolder) convertView.getTag();
				}

				Theme theme = (Theme) getItem(position);	
				holder.volumeBar
						.setOnSeekBarChangeListener(new CustomSeekbarListener(theme));
				holder.mainContainer.setBackgroundResource(getResources()
						.getIdentifier(theme.getImageFile(), "raw",
								getClass().getPackage().getName()));

				holder.mainContainer
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								mainLayoutClicked(getActualPosition(position));
							}
						});

				holder.previousButton
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								previousClicked(position);
							}
						});
				holder.nextButton
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								nextClicked(position);
							}
						});
				if (isPlaying) {
					holder.toggleButton.setImageBitmap(pauseImage);
				} else {
					holder.toggleButton.setImageBitmap(playImage);
				}
				holder.volumeBar.setProgress(theme.getVolume());

			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		public String getTitle(int position) {
			// TODO Auto-generated method stub
			return themes.get(getActualPosition(position)).getTitle();
		}
	}

	public class CatalogAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public CatalogAdapter() {
			mInflater = (LayoutInflater) getParent().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return themes.size();
		}

		public Object getItem(int arg0) {
			Object ret = null;
			if (themes != null && themes.size() > arg0) {
				ret = themes.get(arg0);
			}
			return ret;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {
					convertView = mInflater
							.inflate(R.layout.catalog_item, null);
					holder = new ViewHolder();
					holder.catalogImage = (View) convertView
							.findViewById(R.id.theme_image_view);
					holder.title = (TextView) convertView
							.findViewById(R.id.title_view);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				holder.catalogImage.setBackgroundResource(getResources()
						.getIdentifier(themes.get(position).getImageFile(),
								"raw", getClass().getPackage().getName()));
				holder.title.setText(themes.get(position).getTitle());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			View catalogImage;
			TextView title;
		}
	}

	private void mainLayoutClicked(int position) {
		try {
			TabsController.toggleTabBar(sp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void previousClicked(final int position) {
		try {
			int pos = viewFlow.getSelectedItemPosition();
			if (pos > 0) {
				pos--;
			}
			viewFlow.setSelection(pos);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void nextClicked(final int position) {
		try {
			int pos = viewFlow.getSelectedItemPosition();
			if (pos < viewFlowAdapter.getCount() - 1) {
				pos++;
				viewFlow.setSelection(pos);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void togglePlayClicked() {
		try {
			isPlaying = (!isPlaying);
			viewFlowAdapter.notifyDataSetChanged();
			if (isPlaying) {
				SoundPlayerService.play(sp);
			} else {
				SoundPlayerService.pause(sp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//////////////////////////////////////////SETTING////////////////////////////////////
	private CheckBox checkboxLooping;
	private CheckBox checkboxStopSoundIn;
	private WheelView mHours, mMins;
	private Button buttonSetCancelTimer;
	private int stopInHour,stopInMin;
	private boolean isLoopingEnabled;
	private boolean isStopSoundInEnabled;
	private boolean isTimerEnabled;
	private int loopingTime;
	
	private OnCheckedChangeListener loopingCheckListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
			
			isLoopingEnabled = isChecked;
			sp.edit().putBoolean(Constants.KEY_AUDIO_SHOULD_LOOPING, isLoopingEnabled).commit();
			
			checkboxStopSoundIn.setEnabled(isLoopingEnabled);
			mMins.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
			mHours.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
			buttonSetCancelTimer.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
		}
	};
	
	private OnCheckedChangeListener stopSoundCheckListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
			
			
			isStopSoundInEnabled = isChecked;
			sp.edit().putBoolean(Constants.KEY_AUDIO_LOOPING_STOP_ENABLED, isStopSoundInEnabled).commit();
			
			checkboxStopSoundIn.setEnabled(isLoopingEnabled);
			mMins.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
			mHours.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
			buttonSetCancelTimer.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
		}
	};
	
	private OnClickListener setOrCancelTimerListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			isTimerEnabled = getString(R.string.set_timer).equals(buttonSetCancelTimer.getText().toString());
			sp.edit().putBoolean(Constants.KEY_AUDIO_TIMER_ENABLED, isTimerEnabled).commit();

			if(isTimerEnabled){
				buttonSetCancelTimer.setText(getString(R.string.cancel_timer));
				loopingTime = ((mHours.getCurrentItem()*60) + mMins.getCurrentItem())*60000;
				sp.edit().putInt(Constants.KEY_AUDIO_LOOPING_TIME,loopingTime).commit();   
				 
			}else{
				buttonSetCancelTimer.setText(getString(R.string.set_timer));
			}
		}
	};
	
	private void initSettingsLayout(){
		try {
			checkboxLooping = (CheckBox)findViewById(R.id.check_box_looping);
			checkboxStopSoundIn = (CheckBox)findViewById(R.id.check_box_stop_sound_in);
			buttonSetCancelTimer = (Button)findViewById(R.id.button_set_cancel_timer);
			
			isLoopingEnabled = sp.getBoolean(Constants.KEY_AUDIO_SHOULD_LOOPING, false);
			checkboxLooping.setChecked(isLoopingEnabled);
			
			isStopSoundInEnabled = sp.getBoolean(Constants.KEY_AUDIO_LOOPING_STOP_ENABLED, false);
			checkboxStopSoundIn.setChecked(isStopSoundInEnabled);
			
			isTimerEnabled = sp.getBoolean(Constants.KEY_AUDIO_TIMER_ENABLED, false);
			
			if(isTimerEnabled){
				buttonSetCancelTimer.setText(getString(R.string.cancel_timer));
			}else{
				buttonSetCancelTimer.setText(getString(R.string.set_timer));
			}
			
			loopingTime = sp.getInt(Constants.KEY_AUDIO_LOOPING_TIME, 60000);		
			stopInMin = (loopingTime/60000)%60;
			stopInHour = (loopingTime/60000)/60;		
					
			mHours = (WheelView) findViewById(R.id.hour);
			NumericWheelAdapter hourAdapter = new NumericWheelAdapter(
					getParent(), 0, 23, "%02d hours");
			hourAdapter.setItemResource(R.layout.wheel_text_item);
			hourAdapter.setItemTextResource(R.id.text);
			mHours.setViewAdapter(hourAdapter);
			
			mMins = (WheelView) findViewById(R.id.mins);
			NumericWheelAdapter minAdapter = new NumericWheelAdapter(
					getParent(), 0, 59, "%02d min");
			minAdapter.setItemResource(R.layout.wheel_text_item);
			minAdapter.setItemTextResource(R.id.text);
			mMins.setViewAdapter(minAdapter);
			mMins.setCyclic(true);
			mHours.setCurrentItem(stopInHour);
			mMins.setCurrentItem(stopInMin);
	
			checkboxStopSoundIn.setEnabled(isLoopingEnabled);
			mMins.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
			mHours.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
			buttonSetCancelTimer.setEnabled(isLoopingEnabled&&isStopSoundInEnabled);
			
			checkboxLooping.setOnCheckedChangeListener(loopingCheckListener);
			checkboxStopSoundIn.setOnCheckedChangeListener(stopSoundCheckListener);
			buttonSetCancelTimer.setOnClickListener(setOrCancelTimerListener);
			
			
			
//			mHours.setCurrentItem(calendar.get(Calendar.HOUR_OF_DAY));
//			mMins.setCurrentItem(calendar.get(Calendar.MINUTE));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class ViewFlowViewHolder {
		View mainContainer;
		ImageButton previousButton;
		ImageButton nextButton;
		ImageButton toggleButton;
		SeekBar volumeBar;
	}
	
	class CustomSeekbarListener implements OnSeekBarChangeListener{
		private Theme theme;
		public CustomSeekbarListener(Theme theme) {
			this.theme = theme;
		}
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
//			sp.edit().putInt(Constants.KEY_LAST_PLAYED_SOUND_VOLUMN, volume)
//					.commit();
//			viewFlowAdapter.notifyDataSetChanged();
			String key = "VOLUME"+theme.getSoundFile();
			sp.edit().putInt(key, theme.getVolume()).commit();
			
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			theme.setVolume(progress);
			SoundPlayerService.setVolume(sp, progress);
		}
	}
}